import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Time;
import java.util.Set;


public class GameBlock extends Thread implements KeyListener{
	private int stylerow,stylecol;
	private GamePanel gamePanel;
	private GameTetris gameTetris;
	int speed=GameInfo.time[GameInfo.time.length-1];
	int row,col;
	int[][] last=new int[4][2];
	public GameBlock(int row,int col,int stylerow,int stylecol,GamePanel gamePanel,GameTetris gameTetris){
		  this.col=col;
		  this.row=row;
		  this.gamePanel=gamePanel;
		  this.stylerow=stylerow;
		  this.stylecol=stylecol;
		  this.gameTetris=gameTetris;
		  last[0][0]=-1;
		  this.gamePanel.requestFocusInWindow();
	}
	public void setspeed(int speed) {
		this.speed=speed;
	}
	public void paintBlock(){
		if(last[0][0]!=-1){
			for(int i=0;i<4;i++){
				gamePanel.get_gameBox()[last[i][0]][last[i][1]].setcolor(false);
			}
		}
		int lastbox=0;
		int style=GameInfo.STYLE[stylerow][stylecol];
		for(int i=row+3;i>=row;i--){
			int x=style%16;
			style=style/16;
			for(int j=col+3;j>=col;j--){
				if(x%2==1){
					gamePanel.get_gameBox()[i][j].setcolor(true);
					last[lastbox][0]=i;
					last[lastbox++][1]=j;
					if(row>0)
						gamePanel.get_gameBox()[i-1][j].setcolor(false);
				}
				x=x/2;
			}
		}
	}
	public void run() {
		for(;!is_buttom();row++){
			try {
				Thread.sleep(this.speed);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			this.paintBlock();
		}
	}
	public boolean is_left() {
		 int[][] t=new int[4][2];
		 for(int i=0;i<4;i++){
			 if(last[i][1]==0){
				 return true;
			 }
		 }
		 for(int i=0;i<4;i++){
			 int t_col=last[i][1];
			 int t_row=last[i][0];
			 int j=-1;
			 for(j=-1;;j--){
				 int next_col=t_col+j;
				 int next_row=t_row;
				 int s=0;
				 for(s=0;s<4;s++){
					 if(next_col==last[s][1]&&next_row==last[s][0])
						 break;
				 }
				 if(s==4)
					 break;
			 }
			 if(this.gamePanel.get_gameBox()[t_row][t_col+j].getcolor()==true)
				 return true;
		 }
		 return false;
	}
	public boolean is_right() {
		int[][] t=new int[4][2];
		 for(int i=0;i<4;i++){
			 if(last[i][1]==GameInfo.col-1){
				 return true;
			 }
		 }
		 for(int i=0;i<4;i++){
			 int t_col=last[i][1];
			 int t_row=last[i][0];
			 int j=1;
			 for(j=1;;j++){
				 int next_col=t_col+j;
				 int next_row=t_row;
				 int s=0;
				 for(s=0;s<4;s++){
					 if(next_col==last[s][1]&&next_row==last[s][0])
						 break;
				 }
				 if(s==4)
					 break;
			 }
			 if(this.gamePanel.get_gameBox()[t_row][t_col+j].getcolor()==true)
				 return true;
		 }
		 return false;
	}
	public boolean is_buttom(){
		int[][] t=new int[4][2];
		for(int i=0;i<4;i++){
			if(last[i][0]==19)
				return true;
		}
		for(int i=0;i<4;i++){
			int t_row=last[i][0];
			int t_col=last[i][1];
			int j=0;
			for(j=1;;j++){
				int next_row=t_row+j;
				int next_col=t_col;
				int s=0;
				for(s=0;s<4;s++){
					if(next_row==last[s][0]&&next_col==last[s][1])
						break;
				}
				if(s==4)
					break;
			}
			if(this.gamePanel.get_gameBox()[t_row+j][t_col].getcolor()==true)
				return true;
		}
		return false;
	}
	public void move(int movestep) {
		col+=movestep;
	} 
	public boolean change() {
		int nextstyle=GameInfo.STYLE[this.stylerow][(this.stylecol+1)%4];
		for(int i=3;i>=0;i--){
			int x=nextstyle%16;
			nextstyle=nextstyle/16;
			for(int j=3;j>=0;j--){
				if(x%2==1){
					if(this.col+j>=GameInfo.col||this.row+i>=GameInfo.row)
						return false;
					int s=0;
					for(s=0;s<4;s++){
						if(last[s][0]==this.row+i&&last[s][1]==this.col+j){
							break;
						}
					}
					if(s==4){
						if(gamePanel.get_gameBox()[this.row+i][this.col+j].getcolor()==true){
							return false;
						}
					}
				}
				x=x/2;
			}
		}
		return true;
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		switch (e.getKeyCode()) {
		case KeyEvent.VK_SPACE:
			if(change())
				this.stylecol=(this.stylecol+1)%4;
			break;
		case KeyEvent.VK_LEFT:
			if(!is_left()){
				this.move(-1);
			}
			break;
		case KeyEvent.VK_RIGHT:
			if(!is_right()){
				this.move(1);
			} 
			break;
		case KeyEvent.VK_DOWN:
			this.setspeed(80);
			break;
		default:
			break;
		}
	}
	@Override 
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		switch (e.getKeyCode()) {
		case KeyEvent.VK_DOWN:
			this.setspeed(GameInfo.time[4]);
			break;
		default:
			break;
		}
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
